﻿//文件指定编译编码UTF-8
#pragma execution_character_set("utf-8")

/**
*    @file:    jwtutils.cpp
*    @brief:
*    @author:  sanshi522@163.com
*    @date:    2024-11-04  11:34
*/
#include "qjwtutils.h"
#include "qjsonwebtoken.h"

#include <QDateTime>
#include <QMetaProperty>
#include <QDebug>

#define ACCESS_TOKEN_SECRET "access_token_secret"
#define REFRESH_TOKEN_SECRET "refresh_token_secret"

QJwtUtils::QJwtUtils()
{

}

QJwtUtils::~QJwtUtils()
{

}

QString QJwtUtils::generateAccessToken(const QJsonObject &object)
{
    QJsonWebToken jwt;
    jwt.setSecret(ACCESS_TOKEN_SECRET);
    // set a default payload
    jwt.appendClaim("iss", QString("juangburgos"));
    jwt.appendClaim("iat", QString::number(QDateTime::currentDateTime().toSecsSinceEpoch()));
    jwt.appendClaim("exp", QString::number(QDateTime::currentDateTime().addDays(7).toSecsSinceEpoch()));
    jwt.appendClaim("aud", QString("everybody"));
    jwt.appendClaim("sub", QString("hey there"));
    //set Algorithm
    jwt.setAlgorithmStr("HS256");
    //set object
    for (QString key : object.keys()) {
        if (key == "objectName") {
            continue;
        }
        QJsonValue value = object.value(key);
        jwt.appendClaim(key, value);
    }
    return jwt.getToken();
}

QString QJwtUtils::generateRefreshToken(const QJsonObject &object)
{
    QJsonWebToken jwt;
    jwt.setSecret(REFRESH_TOKEN_SECRET);
    // set a default payload
    jwt.appendClaim("iss", QString("juangburgos"));
    jwt.appendClaim("iat", QString::number(QDateTime::currentDateTime().toSecsSinceEpoch()));
    jwt.appendClaim("exp", QString::number(QDateTime::currentDateTime().addDays(7).toSecsSinceEpoch()));
    jwt.appendClaim("aud", QString("everybody"));
    jwt.appendClaim("sub", QString("hey there"));
    //set Algorithm
    jwt.setAlgorithmStr("HS256");
    //set object
    for (QString key : object.keys()) {
        if (key == "objectName") {
            continue;
        }
        QJsonValue value = object.value(key);
        jwt.appendClaim(key, value);
    }
    return jwt.getToken();
}

QJsonObject QJwtUtils::verifyAccessToken(QString &strToken)
{
    QJsonWebToken token = QJsonWebToken::fromTokenAndSecret(strToken, ACCESS_TOKEN_SECRET);
    // get decoded header and payload
    QString strHeader = token.getHeaderQStr();
    QString strPayload = token.getPayloadQStr();
    QJsonParseError jsonpe;
    QJsonDocument jsonDocument = QJsonDocument::fromJson(strPayload.toUtf8(), &jsonpe);
    if(jsonpe.error == QJsonParseError::NoError){
        QJsonObject data = jsonDocument.object();
        return data;
    }else{
        return QJsonObject();
    }
}

QJsonObject QJwtUtils::verifyRefreshToken(QString &strToken)
{
    QJsonWebToken token = QJsonWebToken::fromTokenAndSecret(strToken, REFRESH_TOKEN_SECRET);
    // get decoded header and payload
    QString strHeader = token.getHeaderQStr();
    QString strPayload = token.getPayloadQStr();
    QJsonParseError jsonpe;
    QJsonObject data = QJsonDocument::fromJson(strPayload.toUtf8(), &jsonpe).object();
    return data;
}
